package com.point.community;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.demo.SysFunctionInterface;
import com.alibaba.dubbo.demo.SysRoleFuncInterface;
import com.alibaba.dubbo.demo.SysUserRoleInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysFunction;
import com.point.community.bean.dwzPageData;

@SuppressWarnings("serial")
public class SysFuncAction extends JsonAction {
	Logger logger = Logger.getLogger(SysFuncAction.class);
	@Autowired
	private SysFunctionInterface sysFuncService;
	@Autowired
	private SysRoleFuncInterface sysRoleFuncService;
	@Autowired
	private SysUserRoleInterface sysUserRoleService;
	
	public SysUserRoleInterface getSysUserRoleService() {
		return sysUserRoleService;
	}

	public void setSysUserRoleService(SysUserRoleInterface sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}

	private String msg = "";
	private boolean success = false;
	private String key = "";
	private String obj = "";
	private PageBean pageBean;
	private String statusCode,message,navTabId,rel,callbackType,forwardUrl,confirmMsg;
	
	private List<SysFunction> sysFunctionList = new ArrayList<SysFunction>();
		
	public SysFunctionInterface getSysFuncService() {
		return sysFuncService;
	}

	public void setSysFuncService(SysFunctionInterface sysFuncService) {
		this.sysFuncService = sysFuncService;
	}

	public SysRoleFuncInterface getSysRoleFuncService() {
		return sysRoleFuncService;
	}

	public void setSysRoleFuncService(SysRoleFuncInterface sysRoleFuncService) {
		this.sysRoleFuncService = sysRoleFuncService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}


	
	public List<SysFunction> getSysFunctionList() {
		return sysFunctionList;
	}

	public void setSysFunctionList(List<SysFunction> sysFunctionList) {
		this.sysFunctionList = sysFunctionList;
	}

	public String jsonQuerySysFunction()
	{
		int upperid = -1;
		if(request.getParameterValues("upperid")!=null)
			upperid = getParameterInteger("upperid");
		sysFunctionList = sysFuncService.queryAllSysFuncList(upperid);
		boolean ret = sysFunctionList==null?false:sysFunctionList.size()>0;
		if (ret) 
		{
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String webQuerySysFunction()
	{
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
		numPerPage = "50";
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
		
		String start = getParameterString("start");
		if (start != null && !start.equals("")) {
			pageBean = new PageBean(start, PAGE_MAX_RECORD_COUNT);
		} else {
			pageBean = new PageBean("0", PAGE_MAX_RECORD_COUNT);
		}
		//参数
		int upperid = -1;
		if(request.getParameterValues("upperid")!=null)
			upperid = getParameterInteger("upperid");
		
		pageBean = sysFuncService.queryAllSysFunc(pageBean, upperid);
		
		sysFunctionList = pageBean.getResultList();

		request.getSession().setAttribute("sysFunctionList", sysFunctionList);

		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = Integer.valueOf(nCurrentPage).toString();
		dwz.numPerPage =  Integer.valueOf(nNumPerPage).toString();
		dwz.totalCount = (long) pageBean.getTotal();
		request.getSession().setAttribute("funcDwzData", dwz);
		
		return "sysFunc";
	}
	/*
	public String jsonQuerySysRoleFunc()
	{
		int roleid = -1;
		if(request.getParameterValues("roleid")!=null)
			roleid = getParameterInteger("roleid");
		logger.error("jsonQuerySysRoleFunc roleid="+roleid);
		List<SysFunction> sysFuncTempList = sysFuncService.queryAllSysFuncList(-1);
		List<SysRoleFunc> sysRoleFuncList = sysRoleFuncService.queryAllSysRoleFuncList(roleid);

		for(int i=0;i<sysFuncTempList.size();i++)
		{
			SysFunction item = sysFuncTempList.get(i);
			if(item.getUpperid()!=0)
				continue;
			sysFunctionList.add(item);
			for(int j=0;j<sysFuncTempList.size();j++)
			{
				SysFunction itemChild = new SysFunction();
				itemChild = sysFuncTempList.get(j);
				
				if(itemChild.getUpperid()==item.getFuncid())
				{
					SysRoleFunc roleFunc = GetRightFunc(itemChild.getFuncid(),sysRoleFuncList);
					if(roleFunc==null)
					{	
						logger.error("roleFunc==null");
						itemChild.setLevel(0);
						itemChild.setRighttype(0);
					}
					else
					{
						logger.error(+roleFunc.getRoleid()+"\t"+roleFunc.getFuncid()+"\t"+roleFunc.getFuncname()+"\t"+roleFunc.getDataright()+"\t");
						itemChild.setLevel(1);
						itemChild.setRighttype(roleFunc.getDataright());
					}
					sysFunctionList.add(itemChild);
				}
			}
		}
		
		success = true;
		msg = "请求成功！";
		return SUCCESS;
	} 
	
	private boolean AppendFuncItem(List<SysRoleFunc> sysUserFuncList,SysRoleFunc item)
	{
		if(sysUserFuncList.size()<1)
		{
			sysUserFuncList.add(item);
			return true;
		}
		for(int i=0;i<sysUserFuncList.size();i++)
		{
			if(sysUserFuncList.get(i).getFuncid()==item.getFuncid())
			{
				if(item.getDataright()==1)
					sysUserFuncList.get(i).setDataright(1);
				return true;
			}
		}
		sysUserFuncList.add(item);
		return true;
	}
	
	private List<SysRoleFunc> getUserFuncList(int userid)
	{
		List<SysRoleFunc> sysUserFuncList = new ArrayList<SysRoleFunc>();
		List<SysUserRole> rolelist = sysUserRoleService.queryAllSysUserRoleList(userid, "", "", 0, "", "");
		for(SysUserRole role:rolelist)
		{
			List<SysRoleFunc> sysRoleFuncList = sysRoleFuncService.queryAllSysRoleFuncList(role.getRoleid());
			for(SysRoleFunc func:sysRoleFuncList)
				AppendFuncItem(sysUserFuncList,func);
		}
		
		return sysUserFuncList;
	} */
	
	
}
